home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Dev / GNU-SMALLTALK.lha / LinkedList.st < prev    next >
Text File  |  1992-02-15  |  4KB  |  155 lines

  1. "======================================================================
  2. |
  3. |   LinkedList Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         16 Mar 91      Class creation now separate statement.
  34. |
  35. | sbb         21 Sep 90      Removed printOn: method; the one from Collection
  36. |              should be ok.  Also, the storeOn: method from
  37. |              Collection should be ok.
  38. |
  39. | sbyrne     25 Apr 89      created.
  40. |
  41. "
  42.  
  43. SequenceableCollection subclass: #LinkedList
  44.                instanceVariableNames: 'firstLink lastLink'
  45.                classVariableNames: ''
  46.                poolDictionaries: ''
  47.                category: nil
  48. !
  49.  
  50. LinkedList comment:
  51. 'I provide methods that access and manipulate linked lists.  I assume that 
  52. the elements of the linked list are subclasses of Link, because I use 
  53. the methods that class Link supplies to implement my methods.' !
  54.  
  55. !LinkedList methodsFor: 'accessing'!
  56.  
  57. at: index
  58.     "Return the element that is index into the linked list."
  59.     | i element |
  60.     i _ 1.
  61.     element _ firstLink.
  62.     [element isNil] whileFalse:
  63.         [ i = index ifTrue: [ ^element ].
  64.       i _ i + 1.
  65.       element _ element nextLink ].
  66.     ^self error: 'index out of bounds in linked list'
  67. !
  68.  
  69. at: index put: object
  70.     self error: 'Do not store into a LinkedList using at:put:'
  71. !!
  72.  
  73.  
  74.  
  75. !LinkedList methodsFor: 'adding'!
  76.  
  77. add: aLink
  78.     "Add aLink at the end of the list; return aLink."
  79.     self addLast: aLink.
  80.     ^aLink
  81. !
  82.  
  83. addFirst: aLink
  84.     "Add aLink at the head of the list; return aLink."
  85.     lastLink isNil ifTrue: [ lastLink _ aLink ].
  86.     aLink nextLink: firstLink.
  87.     firstLink _ aLink.
  88.     ^aLink
  89. !
  90.  
  91. addLast: aLink
  92.     "Add aLink at then end of the list; return aLink."
  93.     firstLink isNil ifTrue: [ firstLink _ aLink ].
  94.     lastLink notNil ifTrue: [ lastLink nextLink: aLink ].
  95.     lastLink _ aLink.
  96.     ^aLink
  97. !
  98.  
  99. removeFirst
  100.     "Remove the first element from the list and return it, or error if the
  101.     list is empty."
  102.  
  103.     ^self remove: firstLink
  104.           ifAbsent: [ ^self error: 'attempted to remove from an empty list' ]
  105. !    
  106.  
  107. removeLast
  108.     "Remove the final element from the list and return it, or error if the
  109.     list is empty."
  110.  
  111.     ^self remove: lastLink
  112.           ifAbsent: [ ^self error: 'attempted to remove from an empty list' ]
  113. !
  114.  
  115. remove: aLink ifAbsent: aBlock
  116.     "Remove aLink from the list and return it, or invoke aBlock if it's not
  117.     found in the list."
  118.     | temp |
  119.     aLink == firstLink
  120.         ifTrue: [ firstLink _ firstLink nextLink.
  121.               firstLink isNil ifTrue: [ lastLink _ nil ] ]
  122.     ifFalse: [ temp _ firstLink.
  123.                [ temp isNil ifTrue: [ ^aBlock value ].
  124.              temp nextLink == aLink ] whileFalse:
  125.                  [ temp _ temp nextLink ].
  126.            temp nextLink: aLink nextLink.
  127.            aLink == lastLink ifTrue: [ lastLink _ temp ] ].
  128.     aLink nextLink: nil.
  129.     ^aLink
  130. !!
  131.  
  132.  
  133.  
  134. !LinkedList methodsFor: 'enumerating'!
  135.  
  136. do: aBlock
  137.     | aLink |
  138.     aLink _ firstLink.
  139.     [ aLink notNil] whileTrue:
  140.         [ aBlock value: aLink.
  141.       aLink _ aLink nextLink]
  142. !!
  143.  
  144.  
  145.  
  146. !LinkedList methodsFor: 'testing'!
  147.  
  148. isEmpty
  149.     "Returns true if the list contains no members"
  150.     ^firstLink isNil
  151. !!
  152.  
  153.